<!-- src/views/business/product/index.vue -->
<template>
  <div class="product-management">
    <div class="page-header">
      <h1>产品管理</h1>
      <p>管理系统产品信息</p>
    </div>

    <el-card>
      <template #header>
        <div class="card-header">
          <span>产品列表</span>
          <el-button type="primary">
            <el-icon><Plus /></el-icon>
            新增产品
          </el-button>
        </div>
      </template>
      
      <el-table :data="productList" v-loading="loading">
        <el-table-column type="index" label="ID" width="60" />
        <el-table-column prop="name" label="产品名称" />
        <el-table-column prop="category" label="分类" />
        <el-table-column prop="price" label="价格">
          <template #default="{ row }">
            ¥{{ row.price }}
          </template>
        </el-table-column>
        <el-table-column prop="stock" label="库存" />
        <el-table-column prop="status" label="状态">
          <template #default="{ row }">
            <el-tag :type="row.status === 'on' ? 'success' : 'danger'">
              {{ row.status === 'on' ? '上架' : '下架' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button size="small" @click="handleEdit(row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.current"
          v-model:page-size="pagination.size"
          :total="pagination.total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

const loading = ref(false)
const productList = ref([])

const pagination = reactive({
  current: 1,
  size: 10,
  total: 0
})

const fetchProducts = async () => {
  loading.value = true
  try {
    // 模拟数据
    productList.value = [
      {
        id: 1,
        name: 'iPhone 15',
        category: '手机',
        price: 5999,
        stock: 100,
        status: 'on',
        createTime: '2024-01-01 10:00:00'
      },
      {
        id: 2,
        name: 'MacBook Pro',
        category: '电脑',
        price: 12999,
        stock: 50,
        status: 'on',
        createTime: '2024-01-02 14:30:00'
      }
    ]
    pagination.total = 2
  } catch (error) {
    console.error('获取产品列表失败:', error)
  } finally {
    loading.value = false
  }
}

const handleEdit = (row) => {
  ElMessage.info(`编辑产品: ${row.name}`)
}

const handleDelete = (row) => {
  ElMessage.info(`删除产品: ${row.name}`)
}

onMounted(() => {
  fetchProducts()
})
</script>

<style scoped>
.product-management {
  padding: 0;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination-container {
  margin-top: 16px;
  display: flex;
  justify-content: flex-end;
}
</style>